//% color="#4169E1" iconWidth=50 iconHeight=40
namespace siot {
    //% block="SIoT 初始化参数 账号[SSID]密码[PASS]服务器地址[IP]" blockType="command" 
    //% SSID.shadow="string" SSID.defl="siot"
    //% PASS.shadow="string" PASS.defl="dfrobot"
    //% IP.shadow="string" IP.defl="192.168."
    export function siotInit(parameter: any, block: any) {
        let ssid = parameter.SSID.code;
        let pass = parameter.PASS.code;
        let ip = parameter.IP.code;

        Generator.addImport("from siot import iot");
        Generator.addDeclaration("siotInit", `_siot_connected = False
def _siot_callback(_siot_topic, _siot_msg):
    _siot_topic = str(_siot_topic, "utf-8")
    _siot_msg = str(_siot_msg, "utf-8")    
    _siot_recv_mess(_siot_topic,_siot_msg)`);
 
        Generator.addCode(`_siot = iot('', ${ip}, user=${ssid}, password=${pass})
_siot.set_callback(_siot_callback)`);

    }
    //% block="连接SIoT" blockType="command"    
    export function siotconnect(parameter: any, block: any) {
        Generator.addCode(`try:
    _siot.connect()
    _siot_connected = True
except OSError as err:
    print("OSError: {}".format(err))`);
    }
    //% block="SIoT断开连接" blockType="command"  
    export function siotstopconnect(parameter: any, block: any) {
        Generator.addCode(`_siot.stop()
_siot_connected = False`);
    }
    //% block="订阅主题[TOPIC]" blockType="command" 
    //% TOPIC.shadow="string" TOPIC.defl="topic1"  
    export function siotgetsubscribe(parameter: any, block: any) {
        let topic = parameter.TOPIC.code;
        Generator.addCode(`_siot.getsubscribe(${topic})`);
        Generator.addInitTail("loop", "_siot.loop()");
    }
    //% block="SIoT发送消息[MESS]到主题[TOPIC]  " blockType="command" 
    //% MESS.shadow="string" MESS.defl="hello"  
    //% TOPIC.shadow="string" TOPIC.defl="topic1"    
    export function publish(parameter: any, block: any) {
        let mess = parameter.MESS.code;
        let topic = parameter.TOPIC.code;

        Generator.addCode(`_siot.publish(str(${topic}), str(${mess}).encode("utf-8"))`);
    }
    //% block="SIoT发送消息[MESS]到主题[TOPIC]并存储(SIoT V2)" blockType="command" 
    //% MESS.shadow="string" MESS.defl="hello"  
    //% TOPIC.shadow="string" TOPIC.defl="topic1"    
    export function publishsave(parameter: any, block: any) {
        let mess = parameter.MESS.code;
        let topic = parameter.TOPIC.code;
        Generator.addCode(`_siot.publish(str(${topic}), (str("->")+str(${mess})).encode("utf-8"))`);
    }
    //% block="当从SIoT主题收到消息时" blockType="hat"  

    export function _siot_recv_(parameter: any, block: any) {

        Generator.addImport("import ubinascii");
        // Generator.addDeclaration(`list(${topic})`,`_siot_topic_list.append("${topic}")`);  
        Generator.addEvent(`_siot_recv_`, `_siot_recv_mess`, `_siot_topic,_siot_msg`);

    }

    //% block="SIoT消息" blockType="reporter""   
    export function _siot_mess(parameter: any, block: any) {
        Generator.addCode(`_siot_msg`);
    }
  //% block="SIoT主题" blockType="reporter""   
  export function _siot_top(parameter: any, block: any) {
    Generator.addCode(`_siot_topic`);
}
    function convertToHex(str: string): string {
        let hex = "";
        for (let i = 0; i < str.length; i++) {
            hex += str.charCodeAt(i).toString(16);
        }
        return hex;
    }
}
